package com.example.arithmeticleetcode.leetcode.September;

import java.util.*;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-09-17 09:25
 **/
public class Demo14 {

    public static void main(String[] args) {
        String[] logs = {"a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"};
        String[] strings = reorderLogFiles(logs);
        System.out.println(Arrays.toString(strings));//["let1 art can","let3 art zero","let2 own kit dig","dig1 8 1 5 1","dig2 3 6"]

        System.out.println("--------------------------");
        String[] logs1 = {"a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo","a2 act car"};
        String[] strings1 = reorderLogFiles(logs1);
        System.out.println(Arrays.toString(strings1));//["a2 act car","g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"]

        System.out.println("--------------------------");
        String[] logs2 = {"j mo", "5 m w", "g 07", "o 2 0", "t q h"};
        String[] strings2 = reorderLogFiles(logs2);
        System.out.println(Arrays.toString(strings2));//[["5 m w","j mo","t q h","g 07","o 2 0"]]

        System.out.println( Character.getNumericValue('Z'));

    }

    public static String[] reorderLogFiles(String[] logs) {
        LinkedList<String> linkedList = new LinkedList<>();
        Map<String, List<String>> map = new HashMap<>();
        for (String log : logs) {
            String[] split = log.split(" ");
            int numericValue = Character.getNumericValue(split[1].charAt(0));
            if (numericValue >= 0 && numericValue <= 9) {
                linkedList.addLast(log);
            } else {
                StringBuilder stringBuilder = new StringBuilder();
                int startIndex = 0;
                if (isNumberAndLetter(split[0])) {
                    startIndex = 1;
                }
                for (int i = 0, len = split.length; i < len; i++) {
                    stringBuilder.append(split[i]);
                }
                if (map.containsKey(stringBuilder.toString())) {
                    List<String> stringList = map.get(stringBuilder.toString());
                    stringList.add(log);
                    map.put(stringBuilder.toString(), stringList);
                } else {
                    List<String> stringList = new ArrayList<>();
                    stringList.add(log);
                    map.put(stringBuilder.toString(), stringList);
                }
            }
        }
        ArrayList<String> strings1 = new ArrayList<>(map.keySet());
        Collections.sort(strings1);
        for (int i = strings1.size() - 1; i >= 0; i--) {
            for (String s : map.get(strings1.get(i))) {
                linkedList.addFirst(s);
            }
        }
        return linkedList.stream().toArray(String[]::new);
    }

    private static boolean isNumberAndLetter(String s) {
        boolean numFlag = false;
        boolean letterFlag = false;
        for (int i = 0, len = s.length(); i < len; i++) {
            int numericValue = Character.getNumericValue(s.charAt(i));
            if ( numericValue >=0 && numericValue < 10) {
                numFlag = true;
            } else {
                letterFlag = true;
            }
            if (numFlag && letterFlag) {
                return true;
            }
        }
        return false;
    }
}
